package com.ef.util;

import org.hibernate.Criteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;

import com.ef.domain.Leads;

public class LeadsExample extends CustomExample<Leads> {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3481802116789877101L;
	protected static final org.apache.log4j.Logger log = org.apache.log4j.Logger
			.getLogger(LeadsExample.class);

	public LeadsExample(Leads entity, Class<Leads> entityClass) {
		super(entity, entityClass);
		this.excludeProperty("isVIP");
		this.excludeProperty("needJudgeNetLevel");
		this.excludeProperty("netLevel");
		this.excludeProperty("netLevelUnit");
		this.excludeProperty("freeF2FCount");
		this.excludeProperty("finishedOriginalInterview");
		this.excludeProperty("cancelTimesInWeek");
		this.excludeProperty("cancelTimes");
		this.excludeProperty("absentTimesInWeek");
		this.excludeProperty("absentTimes");
		this.excludeProperty("isStudent");
		this.excludeProperty("needPunish");
		this.excludeProperty("tmCall");
		this.excludeProperty("crCall");
		this.excludeProperty("ccCall");
	}

	public LeadsExample(Leads entity) {
		super(entity, NoVale);
		this.excludeProperty("isVIP");
		this.excludeProperty("needJudgeNetLevel");
		this.excludeProperty("netLevel");
		this.excludeProperty("netLevelUnit");
		this.excludeProperty("freeF2FCount");
		this.excludeProperty("finishedOriginalInterview");
		this.excludeProperty("cancelTimesInWeek");
		this.excludeProperty("cancelTimes");
		this.excludeProperty("absentTimesInWeek");
		this.excludeProperty("absentTimes");
		this.excludeProperty("isStudent");
		this.excludeProperty("needPunish");
	};

	/**
	 * ʵ��Leads��ͨ�ò�ѯ����,Ҫ��view���entity�� set
	 * 
	 */
	@Override
	public void appendCondition(Criteria criteria) {
		this.excludeProperty("name");
		this.excludeProperty("telephone");
		if (entity.getName() != null && entity.getName().trim().length() > 0) {
			criteria.add(Restrictions.like("name", entity
					.getName().trim(), MatchMode.ANYWHERE));
		}
		if (entity.getTelephone() != null
				&& entity.getTelephone().trim().length() > 0) {
			Disjunction or = Restrictions.disjunction();
			Property myProperty = Property.forName("telephone");
			or.add(myProperty.like(entity.getTelephone().trim(),
					MatchMode.ANYWHERE));
			myProperty = Property.forName("mobileTelephone");
			or.add(myProperty.like(entity.getTelephone().trim(),
					MatchMode.ANYWHERE));
			myProperty = Property.forName("companyTelephone");
			or.add(myProperty.like(entity.getTelephone().trim(),
					MatchMode.ANYWHERE));
			myProperty = Property.forName("otherTelephone");
			or.add(myProperty.like(entity.getTelephone().trim(),
					MatchMode.ANYWHERE));
			criteria.add(or);
		}
		if (entity.getAdChannel() != null
				&& entity.getAdChannel().getName() != null
				&& entity.getAdChannel().getName().trim().length() > 0) {
			criteria.createCriteria("adChannel").add(Restrictions.like("name", entity
							.getAdChannel().getName().trim(),
							MatchMode.ANYWHERE));
		}
		if (entity.getMarketingPlan() != null
				&& entity.getMarketingPlan().getName() != null
				&& entity.getMarketingPlan().getName().trim().length() > 0) {
			criteria.createCriteria("marketingPlan").add( Restrictions.like("name", entity
							.getMarketingPlan().getName().trim(),
							MatchMode.ANYWHERE));
		}
		if (entity.getPte() != null && entity.getPte().getName() != null
				&& entity.getPte().getName().trim().length() > 0) {
			criteria.createCriteria("pte")
					.add( Restrictions.like("name", entity
													.getPte().getName().trim(),
													MatchMode.ANYWHERE) );
		}
		if (entity.getPteLeader() != null
				&& entity.getPteLeader().getName() != null
				&& entity.getPteLeader().getName().trim().length() > 0) {
			criteria.createCriteria("pteLeader").add( Restrictions.like("name", entity
							.getPteLeader().getName().trim(),
							MatchMode.ANYWHERE));
		}
		if (entity.getCourse() != null && entity.getCourse().getName() != null
				&& entity.getCourse().getName().trim().length() > 0) {
			criteria.createCriteria("course").add(Restrictions.like("name", entity
							.getCourse().getName().trim(),
							MatchMode.ANYWHERE));
		}
		if (entity.getCc() != null && entity.getCc().getName() != null
				&& entity.getCc().getName().trim().length() > 0) {
			criteria.createCriteria("cc")
					.add( Restrictions.like("name", entity
													.getCc().getName().trim(),
													MatchMode.ANYWHERE));
		}
		if (entity.getCr() != null && entity.getCr().getName() != null
				&& entity.getCr().getName().trim().length() > 0) {
			criteria.createCriteria("cr")
					.add( Restrictions.like("name", entity
													.getCr().getName().trim(),
													MatchMode.ANYWHERE));
		}
		if (entity.getTm() != null && entity.getTm().getName() != null
				&& entity.getTm().getName().trim().length() > 0) {
			criteria.createCriteria("tm")
					.add(Restrictions.like("name", entity
													.getTm().getName().trim() ,
													MatchMode.ANYWHERE));
		}
		if (entity.getCreateUser() != null
				&& entity.getCreateUser().getName() != null
				&& entity.getCreateUser().getName().trim().length() > 0) {
			criteria.createCriteria("createUser").add(
					 Restrictions.like("name", entity
							.getCreateUser().getName().trim() ,
							MatchMode.ANYWHERE) );
		}
		if (entity.getImportUser() != null
				&& entity.getImportUser().getName() != null
				&& entity.getImportUser().getName().trim().length() > 0) {
			criteria.createCriteria("importUser").add(Restrictions.like("name", entity
							.getImportUser().getName().trim() ,
							MatchMode.ANYWHERE));
		}
		// ͨ��ѡ����,ͨ��ID��ȡ
		if (entity.getMarketingPolicy() != null
				&& entity.getMarketingPolicy().getId() != null) {
			criteria.createCriteria("marketingPolicy").add(
					Restrictions.eq("id", entity.getMarketingPolicy().getId()));
		}
		if (entity.getCurrentlevel() != null
				&& entity.getCurrentlevel().getId() != null) {
			criteria.createCriteria("currentlevel").add(
					Restrictions.eq("id", entity.getCurrentlevel().getId()));
		}
		if (entity.getSchool() != null && entity.getSchool().getId() != null) {
			criteria.createCriteria("school").add(
					Restrictions.eq("id", entity.getSchool().getId()));
		}
		//cyg100319，市场人员查询时，通过Team进行区别
		
		//修改,根据updateTime排序
		//criteria.addOrder(Order.desc("updateTime"));
		// ���ʱ���ѯ
		this.enableLike(MatchMode.ANYWHERE);
		this.appendBetweenDateProperty(criteria);
	}
}
